package com.lune.job.controller;


import com.lune.job.framework.role.RequireRoles;
import com.lune.job.framework.role.Role;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.User;
import com.lune.job.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/user")
@Api(tags = "管理员用户")

public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "获取所有用户")
    @GetMapping("/")
    public List<User> getAllUser() {
        return userService.list();
    }

    @ApiOperation(value = "获取所有用户分页")
    @GetMapping("/page")
    public ResPageBean getAllUserByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer size,    User user) {
        return userService.getAllUserByPage(currentPage, size, user);
    }

    @ApiOperation(value = "根据用户id查询")
    @GetMapping("/detail")
    public List<User> detail(Integer id) {
        return userService.getUserById(id);
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("/")
    @RequireRoles(type = Role.ADMIN)
    public RespBean addUser(@RequestBody User user) {
        if (userService.save(user)) {
            return RespBean.success("成功");
        }
        return RespBean.error("失败");
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public RespBean deleteUser(@PathVariable Integer id) {
        if (userService.removeById(id)) {
            return RespBean.success("删除用户成功");
        }
        return RespBean.error("删除用户失败");
    }

    @ApiOperation(value = "批量删除用户")
    @DeleteMapping("/")
    public RespBean deleteUserByIds(Integer[] ids) {
        if (userService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除用户成功");
        }
        return RespBean.error("批量删除用户失败");
    }


    @ApiOperation(value = "修改用户")
    @PutMapping("/")
    public RespBean updateUer(@RequestBody User user) {
        if (userService.updateById(user)) {
            return RespBean.success("修改用户成功");
        }
        return RespBean.error("修改用户失败");
    }

    @ApiOperation(value = "条件查询用户")
    @PostMapping("/queryByCon")
    public  List<User> queryByCon(@RequestBody User user){
        return userService.queryByCon(user);
    }



}
